package com.gt.filter;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = Logger.getLogger(GlobalExceptionHandler.class);
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
//        logger.error(ExceptionUtils.getFullStackTrace(e));  // 记录错误信息
//        String msg = e.getMessage();
//        if (msg == null || msg.equals("")) {
//            msg = "服务器出错";
//        }
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("error", msg);
        ModelAndView mv = new ModelAndView();
        if (!(request.getHeader("accept").contains("application/json") || (request.getHeader("X-Requested-With") != null
                && request.getHeader("X-Requested-With").contains("XMLHttpRequest")))) {
            StackTraceElement[] stes = e.getStackTrace();
            long exceptionSeq = System.currentTimeMillis();
            logger.error("GlobalException----------" + exceptionSeq + "-----Begin----------");
            for (StackTraceElement ste : stes) {
                String className = ste.getClassName();
                if (className != null && className.indexOf("gt.") != -1) {
                    logger.error("请求异常-" + "异常名称：" + e.getClass().getName() + "，所属文件：" + ste.getFileName() + "，所属类："
                            + ste.getClassName() + "，调用方法：" + ste.getMethodName() + "，所在代码行：" + ste.getLineNumber());
                }
            }
            logger.error(e.getMessage());
            logger.error("GlobalException----------" + exceptionSeq + "-----End------------\n");
            mv.setViewName("error/500");
            return mv;
        } else {
            StackTraceElement[] stes = e.getStackTrace();
            long exceptionSeq = System.currentTimeMillis();
            logger.error("GlobalException----------" + exceptionSeq + "-----Begin----------");
            for (StackTraceElement ste : stes) {
                String className = ste.getClassName();
                if (className != null && className.indexOf("gt.") != -1) {
                    logger.error("Ajax请求异常-" + "异常名称：" + e.getClass().getName() + "，所属文件：" + ste.getFileName() + "，所属类："
                            + ste.getClassName() + "，调用方法：" + ste.getMethodName() + "，所在代码行：" + ste.getLineNumber());
                }
            }
            logger.error(e.getMessage());
            logger.error("GlobalException----------" + exceptionSeq + "-----End------------\n");
            return null;
        }
    }
}
